package engine

import (
	gamemap "FlyFight/Internal/GameMap"
	player "FlyFight/Internal/Player"
	"FlyFight/Internal/bag"
	"FlyFight/Internal/game"
	"FlyFight/Internal/room"
	"FlyFight/sql"
	"log"
	"os"
	"os/signal"
)



func initAll() error {

	cherr := make(chan error,2)
	go func(chan error){
		err:=gamemap.LoadAllMap()
		cherr <- err
	}(cherr)
	go func(chan error){
		err:=bag.Init()
		cherr <- err
	}(cherr)

	if err:=sql.Init();err!=nil{
		log.Println("数据库初始化失败")
		return err
	}
	player.Init()
	err:=<-cherr
	if err!=nil{
		log.Println("地图初始化失败")
		return err
	}
	room.Init()
	game.Init()
	return nil
}

func Run()  {
	c:=make(chan os.Signal, 1)
	signal.Notify(c,os.Interrupt)
	if err:=initAll();err!=nil{
		log.Println("初始化失败")
		panic(err)
	}
	var h httpserver
	h.Init()
	go h.Run()
    <-c
}
